<?php
/*
 * @Autor: zs
 * @Date: 2021-05-26 09:12:32
 * @LastEditors: zs
 * @LastEditTime: 2021-05-26 16:03:50
 */
// 应用公共文件

use app\dic\DicPublic;
use Symfony\Component\VarExporter\VarExporter;
use think\exception\HttpResponseException;
use think\facade\Db;
use think\Response;
use util\Excel;

/**
 * 下划线转驼峰
 * @param $str
 * @return null|string|string[]
 */
function lineToHump($str)
{
    $str = preg_replace_callback('/([-_]+([a-z]{1}))/i', function ($matches) {
        return strtoupper($matches[2]);
    }, $str);
    return $str;
}

/**
 * 驼峰转下划线
 * @param $str
 * @return null|string|string[]
 */
function humpToLine($str)
{
    $str = preg_replace_callback('/([A-Z]{1})/', function ($matches) {
        return '_' . strtolower($matches[0]);
    }, $str);
    return $str;
}

/**
 * @description: 成功
 * @param {*} $msg
 * @param {*} $data
 * @return {*}
 */
function success($msg = '', $data = [])
{
    # code...
    $data = [
        'code' => 1,
        'msg' => $msg,
        'time' => time(),
        'data' => $data,
    ];
    return json($data);
}
/**
 * @description: 失败
 * @param {*} $msg
 * @param {*} $data
 * @return {*}
 */
function error($msg = '', $data = [])
{
    $data = [
        'code' => 0,
        'msg' => $msg,
        'time' => time(),
        'data' => $data,
    ];
    return json($data);
}
/**
 * @description: 其他状态
 * @param {*} $msg
 * @param {*} $data
 * @return {*}
 */
function result($code = 0, $msg = '', $data = [])
{
    $data = [
        'code' => $code,
        'msg' => $msg,
        'time' => time(),
        'data' => $data,
    ];
    return json($data);
}
function addonresult($code = 0, $msg = '', $data = [], $type = 'json', array $header = [])
{
    $result = [
        'code' => $code,
        'msg' => $msg,
        'time' => time(),
        'data' => $data,
    ];
    $response = Response::create($result, $type)->header($header);
    throw new HttpResponseException($response);
}
/**
 * 获取真实ip
 */
function getRealIP()
{
    $forwarded = request()->header("x-forwarded-for");
    if ($forwarded) {
        $ip = explode(',', $forwarded)[0];
    } else {
        $ip = request()->ip();
    }
    return $ip;
}

function parseNodeStr($node)
{
    $array = explode('/', $node);
    foreach ($array as $key => $val) {
        if ($key == 0) {
            $val = explode('.', $val);
            foreach ($val as &$vo) {
                $vo = humpToLine(lcfirst($vo));
            }
            $val = implode('.', $val);
            $array[$key] = $val;
        }
    }
    $node = implode('/', $array);
    return $node;
}

/**
 * 控制器首字母边小写
 */
function parse_lower($controller)
{

    $val = explode('.', $controller);
    if (is_array($val)) {
        foreach ($val as &$vo) {
            $vo = humpToLine(lcfirst($vo));
        }
        $controller = implode('.', $val);
    }
    return $controller;
}

if (!function_exists('__url')) {

    /**
     * 构建URL地址
     * @param string $url
     * @param array $vars
     * @param bool $suffix
     * @param bool $domain
     * @return string
     */
    function __url(string $url = '', array $vars = [], $suffix = true, $domain = false)
    {
        return url($url, $vars, $suffix, $domain)->build();
    }
}



if (!function_exists('sysconfig')) {

    /**
     * 获取系统配置信息
     * @param $group
     * @param null $name
     * @return array|mixed
     */
    function sysconfig($group, $name = null)
    {
        $where = ['group' => $group];
        //$value = empty($name) ? Cache::get("sysconfig_{$group}") : Cache::get("sysconfig_{$group}_{$name}");
        if (empty($value)) {
            if (!empty($name)) {
                $where['name'] = $name;
                $value = \app\admin\model\SystemConfig::where($where)->value('value');
                // Cache::tag('sysconfig')->set("sysconfig_{$group}_{$name}", $value, 3600);
            } else {
                $value = \app\admin\model\SystemConfig::where($where)->column('value', 'name');
                // Cache::tag('sysconfig')->set("sysconfig_{$group}", $value, 3600);
            }
        }
        return $value;
    }
}

/**
 * api发起POST请求
 * @param  string  $func     [请求api方法]
 * @param  string  $data     [请求api数据]
 */
function api_post(string $func, $data = [], $header = [])
{
    $url = config('app.api') . $func;
    $output = curl($url, $data, $header);
    return json_decode($output, true);
}

/**
 * CURL请求函数:支持POST及基本header头信息定义
 * @param  string  $api_url      [请求远程链接]
 * @param  array   $post_data    [请求远程数据]
 * @param  array   $header       [头信息数组]
 */
function curl(string $api_url, $post_data = [], $header = [])
{
    /**初始化CURL句柄**/
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api_url);
    /**配置返回信息**/
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //获取的信息以文件流的形式返回，不直接输出
    curl_setopt($ch, CURLOPT_HEADER, 0); //不返回header部分
    /**配置超时**/
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //连接前等待时间,0不等待
    curl_setopt($ch, CURLOPT_TIMEOUT, 60); //连接后等待时间,0不等待。如下载mp3
    /**配置页面重定向**/
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //跟踪爬取重定向页面
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10); //指定最多的HTTP重定向的数量
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
    /**配置Header、请求头、协议信息**/
    $header[] = "CLIENT-IP:" . request()->ip();
    $header[] = "X-FORWARDED-FOR:" . request()->ip();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_ENCODING, ""); //Accept-Encoding编码，支持"identity"/"deflate"/"gzip",空支持所有编码
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Baiduspider/2.0; +" . request()->domain() . request()->domain() . ")");
    //模拟浏览器头信息
    curl_setopt($ch, CURLOPT_REFERER, request()->domain()); //伪造来源地址
    /**配置POST请求**/
    if ($post_data && is_array($post_data)) {
        curl_setopt($ch, CURLOPT_POST, 1); //支持post提交数据
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
    }
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //禁止 cURL 验证对等证书
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //是否检测服务器的域名与证书上的是否一致
    $data = curl_exec($ch);
    if (curl_errno($ch)) {
        // 捕抓异常
        return ['status' => 'error', 'message' => curl_error($ch)];
    } else {
        // 正常返回
        curl_close($ch);
        return $data;
    }
}
/**
 * 返回后台跟目录
 */
function get_back_addons_path()
{

    $back_addons_path = root_path() . "addons";
    // 如果插件目录不存在则创建
    if (!is_dir($back_addons_path)) {
        @mkdir($back_addons_path, 0755, true);
    }
    return $back_addons_path;
}
/**
 * 获取插件所有的配置
 */
function get_addons_all_config($name)
{
    $addon = get_addons_instance($name);
    if (!$addon) {
        return [];
    }
    $config = $addon->getConfig(true);
    return $config;
}
/**
 * 获取插件配置 数组格式
 */
function get_addons_config_format($name)
{
    $configf = [];
    $config = get_addons_all_config($name);
    foreach ($config as $k => $v) {
        $configf[$v['name']] = $v['value'];
    }
    return $configf;
}
/**
 * 设置插件配置
 */
function set_addons_config($name, $config)
{
    $file = get_back_addons_path() . DIRECTORY_SEPARATOR . $name . DIRECTORY_SEPARATOR . "config.php";
    if (!is_file($file)) {
        throw new \Exception("配置文件不存在");
    }
    if (!is_writable($file)) {
        throw new \Exception("配置文件无法写入");
    }
    $res = file_put_contents($file, "<?php\n\n" . "return " . VarExporter::export($config) . ";\n", LOCK_EX);
    if (!$res) {
        throw new \Exception("配置修改失败");
    }
    return true;
}


/**
 * get请求
 */
if (!function_exists('geturl')) {
    function geturl($url, $apikey, $apisecret)
    {
        $headerArray = array("Content-Type:application/json;", 'Authorization:Basic ' . base64_encode("$apikey:$apisecret"));
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}

if (!function_exists('puturl')) {

    function puturl($url, $data = array(), $site)
    {
        $data = (is_array($data)) ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data;
        $apikey = $site['apikey'];
        $apisecret = $site['apisecret'];
        $headerArray = array("Content-Type:application/json;", 'Authorization:Basic ' . base64_encode("$apikey:$apisecret"));
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); //设置请求方式
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
}


if (!function_exists('sku_color_deal')) {

    function sku_color_deal($data = '')
    {
        $color_str = str_replace(' ', '+', $data);
        return $color_str;
    }
}





/**
 * 发送post请求
 * 
 */

if (!function_exists('posturl')) {
    function posturl($url, $data = array(), $iscustom = null)
    {
        $data = (is_array($data)) ? json_encode($data, JSON_UNESCAPED_UNICODE) : $data;
        $apikey = $iscustom['apikey'];
        $apisecret = $iscustom['apisecret'];

        $headerArray = array("Content-Type:application/json;", 'Authorization:Basic ' . base64_encode("$apikey:$apisecret"));
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
}



/**
 * 获取excel路径
 * 
 */
if (!function_exists('excel_url')) {
    function excel_url($url)
    {
        $rst = explode(DicPublic::$base_url, $url);
        return $rst[1];
    }
}


/**
 * 获取excel数据
 */
if (!function_exists('excel_data')) {
    function excel_data($url)
    {
        $rst =  Excel::import($url);
        unset($rst[1]);
        return array_column($rst, 1, 0);
        // return  $rst;
    }
}


/**
 * 封装数据库连接
 */
if (!function_exists('table')) {
    function table($table)
    {
        return Db::name($table);
    }
}


/**
 * 封装mongo数据库连接
 */
if (!function_exists('mgtable')) {
    function mgtable($table)
    {
        return Db::connect('mongo')->table($table);
    }
}




function  get_color_value($output_id, $color_id)

{
    $color_list_value = [
        1 => [1 => '黑色', 2 => '肤色', 3 => '粉色'],
        2 => [1 => '黑色', 2 => '肤色', 3 => '白色'],
        3 => [1 => '黑色', 2 => '肤色', 3 => '粉色'],
        4 => [1 => '黑色', 2 => '肤色'],
        5 => [1 => '黑色', 2 => '肤色', 3 => '粉色', 4 => '紫色'],
        6 => [1 => '黑色', 2 => '肤色'],
        7 => [1 => '黑色', 2 => '肤色'],
        8 => [1 => '黑色', 2 => '蓝色', 3 => '绿色', 4 => '红色'],
        9 => [1 => '黑色', 2 => '肤色', 3 => '咖色'],
        10 => [1 => '黑色', 2 => '肤色', 3 => '粉色'],
        11 => [1 => '黑色', 2 => '肤色', 3 => '白色', 4 => '蕾丝肤色', 5 => '蕾丝红色', 6 => '豹纹', 7 => '印花'],
        12 => [1 => '黑色', 2 => '肤色', 3 => '白色', 4 => '棕色'],
        13 => [1 => '黑色', 2 => '白色', 3 => '灰色'],
        14 => [1 => '黑色', 2 => '肤色', 3 => '灰色'],
        15 => [1 => '黑色', 2 => '肤色'],
        16 => [1 => '黑色', 2 => '肤色', 3 => '白豹', 4 => '棕豹', 5 => '花纹'],
        17 => [1 => '肤色'],
        18 => [1 => '黑色', 2 => '肤色'],
    ];
    return  isset($color_list_value[$output_id][$color_id]) ? $color_list_value[$output_id][$color_id] : '暂无颜色';
}

function get_color_list($output_id)
{
    $color_list = [
        1 => [
            ['id' => 1, 'value' => '黑色'],
            ['id' => 2, 'value' => '肤色'],
            ['id' => 3, 'value' => '粉色'],
        ],
        2 => [
            ['id' => 1, 'value' => '黑色'],
            ['id' => 2, 'value' => '肤色'],
            ['id' => 3, 'value' => '白色'],
        ],
        3 => [
            ['id' => 1, 'value' => '黑色'],
            ['id' => 2, 'value' => '肤色'],
            ['id' => 3, 'value' => '粉色'],
        ],
        4 => [
            ['id' => 1, 'value' => '黑色'],
            ['id' => 2, 'value' => '肤色'],
        ],
        5 => [
            ['id' => 1, 'value' => '黑色'],
            ['id' => 2, 'value' => '肤色'],
            ['id' => 3, 'value' => '粉色'],
            ['id' => 4, 'value' => '紫色'],
        ],
        6 => [
            ['id' => 1, 'value' => '黑色'],
            ['id' => 2, 'value' => '肤色'],
        ],
        7 => [
            ['id' => 1, 'value' => '黑色'],
            ['id' => 2, 'value' => '肤色'],
        ],
        8 => [
            ['id' => 1, 'value' => '黑色'],
            ['id' => 2, 'value' => '蓝色'],
            ['id' => 3, 'value' => '绿色'],
            ['id' => 4, 'value' => '红色']
        ],
        9 => [
            ['id' => 1, 'value' => '黑色'],
            ['id' => 2, 'value' => '肤色'],
            ['id' => 3, 'value' => '咖色'],
        ],
        10 => [
            ['id' => 1, 'value' => '黑色'],
            ['id' => 2, 'value' => '肤色'],
            ['id' => 3, 'value' => '粉色'],
        ],
        11 => [
            ['id' => 1, 'value' => '黑色'],
            ['id' => 2, 'value' => '肤色'],
            ['id' => 3, 'value' => '白色'],
            ['id' => 4, 'value' => '蕾丝肤色'],
            ['id' => 5, 'value' => '蕾丝红色'],
            ['id' => 6, 'value' => '豹纹'],
            ['id' => 7, 'value' => '印花'],
        ],
        12 => [
            ['id' => 1, 'value' => '黑色'],
            ['id' => 2, 'value' => '肤色'],
            ['id' => 3, 'value' => '白色'],
            ['id' => 4, 'value' => '棕色']
        ],
        13 => [
            ['id' => 1, 'value' => '黑色'],
            ['id' => 2, 'value' => '白色'],
            ['id' => 3, 'value' => '灰色'],
        ],
        14 => [
            ['id' => 1, 'value' => '黑色'],
            ['id' => 2, 'value' => '肤色'],
            ['id' => 3, 'value' => '灰色'],
        ],
        15 => [
            ['id' => 1, 'value' => '黑色'],
            ['id' => 2, 'value' => '肤色'],
        ],
        16 => [
            ['id' => 1, 'value' => '黑色'],
            ['id' => 2, 'value' => '肤色'],
            ['id' => 3, 'value' => '白豹'],
            ['id' => 4, 'value' => '棕豹'],
            ['id' => 5, 'value' => '花纹'],
        ],
        17 => [
            ['id' => 1, 'value' => '肤色'],

        ],
        18 => [
            ['id' => 1, 'value' => '黑色'],
            ['id' => 2, 'value' => '肤色'],
        ],
    ];
    return  $color_list[$output_id];
}

//获取数据表表头
function  get_table_header($list)
{

    foreach ($list as $v) {
        if ($v == 'color') {
            $comment = '颜色';
        } elseif ($v == 'batch') {
            $comment = '批次号';
        } elseif ($v == 'order_num') {
            $comment = '快递单号';
        } elseif ($v == 'create_time') {
            $comment = '创建时间';
        } else {
            $comment = $v;
        }
        $new_list[] = [
            'visible' => true,
            'label' => $comment,
            'prop' => $v,
        ];
    }
    return $new_list;
}



//获取下拉框信息
function get_options($color)
{
    $new_color = [];
    foreach ($color as $k => $v) {
        $new_color[] = [
            'id' => $k,
            'value' => $v
        ];
    }
    return [
        'color_list' => $new_color,
        'audit_list' => [
            ['id' => 0, 'value' => '待审核'],
            ['id' => 1, 'value' => '审核通过'],
            ['id' => 2, 'value' => '不通过'],
        ],
    ];
}

function geturl_business($url, $access_token)
{
    $headerArray = array('Access-Token:' . $access_token);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

if (!function_exists('posturl_business')) {
    function posturl_business($url, $access_token, $data = '')
    {
        $data_string = json_encode(['query' => $data]);
        $headerArray = array("Content-Type:application/json;",'X-Shopify-Access-Token:' . $access_token,'Content-Length:' . strlen($data_string));

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

//        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
//        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
//        curl_setopt($curl, CURLOPT_CAINFO, 'D:/phpstudy_pro/Extensions/php/ca-bundle.crt');

        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Errno:' . curl_error($curl);//捕抓异常
        }
        curl_close($curl);
        return $output;
    }
    
//用户邮箱规则6-12位随机英文
function buildRandStr($minLength = 6, $maxLength = 12)
{
    // 生成随机字符串
    $str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    $length = rand($minLength, $maxLength); // 随机生成长度
    $string = substr(str_shuffle($str_pol), 0, $length);
    $string = strtolower($string);
    return $string;
}

}